package com.qf.business.user.controller;

import com.qf.business.user.service.RedEnvelopesService;
import com.qf.commons.web.aop.UserInfo;
import com.qf.data.base.r.Codes;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.base.utils.QfBeanUtils;
import com.qf.data.user.entity.RedEnvelopes;
import com.qf.data.user.vo.input.RedEnveInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * description: 红包相关的Controller
 * author: Ken
 * 公众号：Java架构栈
 */
@RestController
@RequestMapping("/red")
@Slf4j
public class RedEnvelopesController {

    @Autowired
    private RedEnvelopesService envelopesService;

    /**
     * 发送红包
     * @return
     */
    @RequestMapping("/send")
    public R send(@Valid RedEnveInput redEnveInput){
        log.info("[red send] - 发送红包的接口 - {}", redEnveInput);

        RedEnvelopes redEnvelopes = QfBeanUtils.beanCopy(redEnveInput, RedEnvelopes.class);
        long redId = envelopesService.sendRedEnvelopes(redEnvelopes);
        if (redId > 0)
            return Rs.create(redId);
        else if (redId == -1)
            return Rs.createFail(Codes.SEND_RED_ERROR);
        return Rs.createFail(Codes.FAIL);
    }

    /**
     * 抢红包
     * @return
     */
    @RequestMapping("/rob")
    public R rob(Long redid){
        UserInfo.setUid((long) (Math.random() * 100));
        log.info("[rob red] - 抢红包的业务触发 - 红包id：{} - 用户id：{}", redid, UserInfo.getUid());
        long result = envelopesService.robRedEnvelopoes(redid);
        return Rs.create(result);
    }
}
